package com.ztstech.matisse_android_plugin.matisse;

import android.Manifest;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.util.Log;

import com.yanzhenjie.permission.Action;
import com.yanzhenjie.permission.AndPermission;
import com.zhihu.matisse.Matisse;
import com.zhihu.matisse.MimeType;
import com.zhihu.matisse.engine.impl.GlideEngine;
import com.zhihu.matisse.filter.Filter;
import com.zhihu.matisse.internal.entity.CaptureStrategy;
import com.ztstech.matisse_android_plugin.R;
import com.ztstech.matisse_android_plugin.util.LogUtils;

import java.util.List;
import java.util.Set;

import static com.ztstech.matisse_android_plugin.matisse.MetisseConstant.REQUEST_CODE_MATISSE;
import static com.ztstech.matisse_android_plugin.matisse.MetisseConstant.REQUEST_MATISSE_TO_CLIP;


/**
 * Created by smm on 2018/3/9.
 */

public class MatissePhotoHelper {

    public static final String NO_PERMISSION_HINT = "请先开启拍照及读写权限哦～";

    public static String[] PERMISSIONS = {Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA};

    public static void selectPhoto(final Activity activity, final int maxsize, final Set<MimeType> mimeTypeSet, final int screenOrientation, final int spanCount) {
        AndPermission.with(activity).runtime().permission(PERMISSIONS)
                .onDenied(new Action<List<String>>() {
                    @Override
                    public void onAction(List<String> data) {
                        LogUtils.i("权限判断","拒绝");
                        // 判断用户是不是不再显示权限弹窗了，若不再显示的话进入权限设置页
                        if (AndPermission.hasAlwaysDeniedPermission(
                                activity, PERMISSIONS)) {
                            // 打开权限设置页
                            MatisseUtil.getAppDetailSettingIntent(activity);
                        }
                    }
                }).onGranted(new Action<List<String>>() {
            @Override
            public void onAction(List<String> data) {
                LogUtils.i("权限判断","允许");
                Matisse.from(activity)
                        .choose(mimeTypeSet, false)
                        .countable(maxsize > 1)
                        .capture(true)
                        .captureStrategy(
                                new CaptureStrategy(true, activity.getPackageName() + ".fileProvider"))
                        .maxSelectable(maxsize)
                        .addFilter(new MediaFilter(20 * Filter.K * Filter.K, 4))
                        .showSingleMediaType(true)
                        .spanCount(spanCount)
                        .restrictOrientation(screenOrientation)
                        .thumbnailScale(0.85f)
                        .imageEngine(new GlideEngine())
                        .forResult(REQUEST_CODE_MATISSE);
            }
        }).start();
    }

    /**
     * 只选择视频
     * @param activity
     * @param maxsize
     * @param mimeTypeSet
     */
    public static void selectVideo(final Activity activity, final int maxsize,
                                   final Set<MimeType> mimeTypeSet,
                                   final int screenOrientation, final int spanCount) {
        AndPermission.with(activity).runtime().permission(PERMISSIONS)
                .onDenied(new Action<List<String>>() {
                    @Override
                    public void onAction(List<String> data) {
                        LogUtils.i("权限判断","拒绝");
                        // 判断用户是不是不再显示权限弹窗了，若不再显示的话进入权限设置页
                        if (AndPermission.hasAlwaysDeniedPermission(
                                activity, PERMISSIONS)) {
                            // 打开权限设置页
                           MatisseUtil.getAppDetailSettingIntent(activity);
                        }
                    }
                }).onGranted(new Action<List<String>>() {
            @Override
            public void onAction(List<String> data) {
                LogUtils.i("权限判断","允许");
                Matisse.from(activity)
                        .choose(mimeTypeSet, false)
                        .countable(maxsize > 1)
                        .addFilter(new MediaFilter(20 * Filter.K * Filter.K, 5))
                        .showSingleMediaType(true)
                        .spanCount(spanCount)
                        .restrictOrientation(screenOrientation)
                        .maxSelectable(maxsize)
                        .forResult(REQUEST_CODE_MATISSE);
            }
        }).start();
    }

    public static void selectfromalbum(final Activity activity, final int maxsize, final int screenOrientation, final int spanCount) {
        AndPermission.with(activity).runtime().permission(PERMISSIONS)
                .onDenied(new Action<List<String>>() {
                    @Override
                    public void onAction(List<String> data) {
                        LogUtils.i("权限判断","拒绝");
                        // 判断用户是不是不再显示权限弹窗了，若不再显示的话进入权限设置页
                        if (AndPermission.hasAlwaysDeniedPermission(
                                activity, PERMISSIONS)) {
                            // 打开权限设置页
                            MatisseUtil.getAppDetailSettingIntent(activity);
                        }
                    }
                }).onGranted(new Action<List<String>>() {
            @Override
            public void onAction(List<String> data) {
                LogUtils.i("权限判断","允许");
                Matisse.from(activity)
                        .choose(MimeType.ofImage())
                        .theme(R.style.Matisse_Zhihu)
                        .countable(maxsize > 1)
                        .addFilter(new MediaFilter(20 * Filter.K * Filter.K, 5))
                        .showSingleMediaType(true)
                        .maxSelectable(maxsize)
                        .spanCount(spanCount)
                        .restrictOrientation(screenOrientation)
                        .imageEngine(new GlideEngine())
                        .forResult(REQUEST_CODE_MATISSE);
            }
        }).start();
    }

//    public static String handleReturnImagePath(Intent data, int index) {
//        String path = Matisse.obtainPathRduceResult(data).get(index);
//        // 部分网页保存的图片含有webp后缀名 上传到服务器会报错 所以这里把含有webp的转成jpg
//        if (path.contains(".webp")) {
//            File file = BitmapUtil.saveBitmapFile(BitmapFactory.decodeFile(path));
//            path = file.getPath();
//        }
//        return path;
//    }

    /**
     * 选择全部类型文件
     * @param isCanMixSelect 是否可以同时选择图片或者视频
     */
    public static void selectAll(final Activity activity, final int maxImageSize,
                                 final int maxVideoSize, final boolean isCanMixSelect,
                                 final int screenOrientation, final int spanCount) {
        Log.d("zxx","打印包名："+activity.getPackageName());
        AndPermission.with(activity).runtime().permission(PERMISSIONS)
                .onDenied(new Action<List<String>>() {
                    @Override
                    public void onAction(List<String> data) {
                        LogUtils.i("权限判断","拒绝");
                        // 判断用户是不是不再显示权限弹窗了，若不再显示的话进入权限设置页
                        if (AndPermission.hasAlwaysDeniedPermission(
                                activity, PERMISSIONS)) {
                            // 打开权限设置页
                            MatisseUtil.getAppDetailSettingIntent(activity);
                        }
                    }
                }).onGranted(new Action<List<String>>() {
            @Override
            public void onAction(List<String> data) {
                LogUtils.i("权限判断","允许");
                if(isCanMixSelect) {
                    Matisse.from(activity)
                            .choose(MimeType.ofAll(), false)
                            .countable(true)
                            .capture(true)
                            .captureStrategy(
                                    new CaptureStrategy(true, activity.getPackageName() + ".fileProvider"))
//                            .maxSelectablePerMediaType(maxImageSize, maxVideoSize)
                            .maxSelectable(maxImageSize > maxVideoSize ? maxVideoSize : maxImageSize)
                            .addFilter(new MediaFilter(20 * Filter.K * Filter.K, 5))
                            .spanCount(spanCount)
                            .restrictOrientation(screenOrientation)
                            .thumbnailScale(0.85f)
                            .imageEngine(new GlideEngine())
                            .forResult(REQUEST_CODE_MATISSE);
                }else {
                    Matisse.from(activity)
                            .choose(MimeType.ofAll(), true)
                            .countable(true)
                            .capture(true)
                            .captureStrategy(
                                    new CaptureStrategy(true, activity.getPackageName() + ".fileProvider"))
                            .maxSelectablePerMediaType(maxImageSize,maxVideoSize)
                            .addFilter(new MediaFilter(20 * Filter.K * Filter.K, 5))
                            .spanCount(spanCount)
                            .restrictOrientation(screenOrientation)
                            .thumbnailScale(0.85f)
                            .imageEngine(new GlideEngine())
                            .forResult(REQUEST_CODE_MATISSE);
                }
            }
        }).start();
    }


    /**
     * im聊天界面选择图片和视频
     *
     * @param activity
     */
    public static void selectIMPhotoAndVideo(final Activity activity, int screenOrientation, int spanCount) {
        AndPermission.with(activity).runtime().permission(PERMISSIONS)
                .onDenied(new Action<List<String>>() {
                    @Override
                    public void onAction(List<String> data) {
                        LogUtils.i("权限判断","拒绝");
                        // 判断用户是不是不再显示权限弹窗了，若不再显示的话进入权限设置页
                        if (AndPermission.hasAlwaysDeniedPermission(
                                activity, PERMISSIONS)) {
                            // 打开权限设置页
                            MatisseUtil.getAppDetailSettingIntent(activity);
                        }
                    }
                }).onGranted(new Action<List<String>>() {
            @Override
            public void onAction(List<String> data) {
                LogUtils.i("权限判断","允许");
                Matisse.from(activity)
                        .choose(MimeType.ofAll(), false)
                        .countable(false)
                        .capture(false)
                        .captureStrategy(
                                new CaptureStrategy(true, activity.getPackageName() + ".fileProvider"))
                        .maxSelectable(9)
                        .addFilter(new MediaFilter(20 * Filter.K * Filter.K, 5))
                        .spanCount(4)
                        .restrictOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT)
                        .thumbnailScale(0.85f)
                        .imageEngine(new GlideEngine())
                        .forResult(REQUEST_CODE_MATISSE);
            }
        }).start();
    }

    /**
     * 裁剪头像（一张图）
     * @param activity
     * @param mimeTypeSet
     * @param screenOrientation
     * @param spanCount
     */
    public static void clipOnePhoto(final Activity activity, final Set<MimeType> mimeTypeSet, final int screenOrientation, final int spanCount) {
        AndPermission.with(activity).runtime().permission(PERMISSIONS)
                .onDenied(new Action<List<String>>() {
                    @Override
                    public void onAction(List<String> data) {
                        LogUtils.i("权限判断","拒绝");
                        // 判断用户是不是不再显示权限弹窗了，若不再显示的话进入权限设置页
                        if (AndPermission.hasAlwaysDeniedPermission(
                                activity, PERMISSIONS)) {
                            // 打开权限设置页
                            MatisseUtil.getAppDetailSettingIntent(activity);
                        }
                    }
                }).onGranted(new Action<List<String>>() {
            @Override
            public void onAction(List<String> data) {
                LogUtils.i("权限判断","允许");
                Matisse.from(activity)
                        .choose(mimeTypeSet, false)
                        .countable(false)
                        .capture(true)
                        .captureStrategy(
                                new CaptureStrategy(true, activity.getPackageName() + ".fileProvider"))
                        .maxSelectable(1)
                        .addFilter(new MediaFilter(20 * Filter.K * Filter.K, 5))
                        .showSingleMediaType(true)
                        .spanCount(spanCount)
                        .restrictOrientation(screenOrientation)
                        .thumbnailScale(0.85f)
                        .imageEngine(new GlideEngine())
                        .forResult(REQUEST_MATISSE_TO_CLIP);
            }
        }).start();
    }

}
